#include <QFileDialog>
#include <QTextStream>
#include <QLabel>
#include <QThread>
#include <QtDebug>

#include "file_manager.h"
#include "information.h"
#include "data_processing.h"

File_manager::File_manager(QObject* parent)
    : QObject(parent)
    , data_processing_(new Data_processing(nullptr))  // nullptr不能设置为this，否则无法移动到新线程中
{
    setup_connections();

    data_processing_->moveToThread(&thread_);
}

File_manager::~File_manager()
{
    qDebug()<< "File_manager::~File_manager = " << QThread::currentThreadId();

    delete data_processing_;

    thread_.quit();
    thread_.wait();    
}

//QList<Information*>* File_manager::create_dialog_one_thread(QWidget* parent,
//                                                 QLabel* station_label,
//                                                 QString caption)
//{
//    QString file_name_and_path(QFileDialog::getOpenFileName(parent, caption));

//    QList<Information*>* list(nullptr);

//    // 根据选择打开文件
//    if ( !file_name_and_path.isEmpty()) {
//        list = read_file(file_name_and_path);
//    }

//    // 设置抬头显示的台站名称
//    station_label->setText(get_station_name(file_name_and_path));

//    return list;
//}

QList<Information *> *File_manager::open_dialog_for_read_file_in_multithread(QWidget *parent,
                                                                             QString caption)
{
    QString file_name_and_path(QFileDialog::getOpenFileName(parent, caption));

    if ( !file_name_and_path.isEmpty()) {
        if ( !thread_.isRunning()) {
            thread_.start();
            qDebug()<< "thread_.staring()";
        }

        qDebug()<< "File_manager::get_dialog_multithread = " << QThread::currentThreadId();

        // 通过信号槽，将数据传递给在新线程的data_processing进行处理
        emit signal_read_file_from_txt_file(file_name_and_path);
    }

    return nullptr;
}

//bool File_manager::calculate_frequency(double frequency, QList<Information *> *list)
//{
//    bool flag(false);

//    int count(list->count());
//    for (int i(0); i != count; ++i) {
//        double cal = frequency * 2 - list->at(i)->get_frequency();

//        // 遍历台站频率进行互调计算
//        for (int index(0); index != count; ++index) {
//            QString number =
//                    QString::number(qAbs(cal - list->at(index)->get_frequency()), 'f', 3);
//            if (0.000 == number.toDouble()) {
//                list->value(index)->set_is_inter_modulation(true);
//                list->value(index)->set_inter_modulation_frequency(cal);
//            }
//        }

//        // 遍历台站频率进行互调计算
//        cal = list->at(i)->get_frequency() * 2 - frequency;
//        for (int index(0); index != count; ++index) {
//            QString number =
//                    QString::number(qAbs(list->at(index)->get_frequency() - cal), 'f', 3);
//            if (0.000 == number.toDouble()) {
//                list->value(index)->set_is_inter_modulation(true);
//                list->value(index)->set_inter_modulation_frequency(cal);
//            }
//        }

//        flag = true;
//    }

//    return flag;
//}

bool File_manager::calculate_frequency_in_multithread(double frequency, QList<Information *> *list)
{
    // 在新线程中进行互调计算
    emit signal_calculate_frequency_informations(frequency, list);

    return false;
}

void File_manager::setup_connections()
{
    // 向data_processing_传递打开的文件名称及路径
    connect(this, &File_manager::signal_read_file_from_txt_file,
            data_processing_, &Data_processing::read_file);

    // 向data_process_传递互调频率和台站信息，在data_process_中进行互调计算
    connect(this, &File_manager::signal_calculate_frequency_informations,
            data_processing_, &Data_processing::calculate_frequency);

    // 向this返回台站名称，data_processing_在新线程
    connect(data_processing_, &Data_processing::signal_station_name,
            this, &File_manager::signal_station_name);

    // 传递从txt文档中读取的台站信息
    connect(data_processing_, &Data_processing::signal_frequency_informations,
            this, &File_manager::signal_frequency_informations);

    // 向this返回互调计算后的台站频率信息
    connect(data_processing_, &Data_processing::signal_is_inter_modulation,
            this, &File_manager::signal_informations_to_model_view);
}

QList<Information*>* File_manager::read_file(QString file_name_and_path)
{
    QFile file(file_name_and_path);

    if ( file.open(QFile::ReadOnly)) {
        QTextStream in(&file);

        // 编码设置
        in.setCodec("utf-8");
        QString content("");

        QList<Information*>* station_information_list(new QList<Information*>());

        // 遍历文件每一行内容，将内容保存
        while (0 != ((content = in.readLine()).length())) {
            // qDebug()<< content <<endl;
            station_information_list->append(split_data(content));
        }

        return station_information_list;
    }

    // 只读模式打开文件失败
    return nullptr;
}

QString File_manager::get_station_name(QString station_information)
{
    // 获取文件名称，格式为***.txt
    QString content = station_information.split("/").last();

    // 去除“.txt”后缀返回
    return content.replace(".txt", "", Qt::CaseInsensitive);
}

Information *File_manager::split_data(QString content)
{
    QStringList list = content.split("-");

    // 设置数据项
    Information* info(new Information(list[0], list[1].toDouble()));
    info->set_is_inter_modulation(false);
    info->set_inter_modulation_frequency(0.0);

    return info;
}

